package com.margo.project.zookeeper.service;

import java.text.SimpleDateFormat;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.margo.project.zookeeper.config.MargoZookeeperLock;
import com.margo.project.zookeeper.model.MargoBuildPlanVO;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

@Service
public class MargoZookeeperService {
	
	private Logger logger=LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private JdbcTemplate JdbcTemplate;
	@Autowired
	private MargoZookeeperLock margoZookeeperLock;

	/**
	 * 	分布式锁插入
	 * @param margoBuildPlanVO
	 * @param ownedArea
	 * @return
	 */
	public int insertBuildPlan(MargoBuildPlanVO margoBuildPlanVO, String ownedArea) {
		margoZookeeperLock.lock(ownedArea);
		try {
			RowMapper<MargoBuildPlanVO> rowMapper = new BeanPropertyRowMapper<MargoBuildPlanVO>(MargoBuildPlanVO.class);
			Object[] args = new Object[2];

			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			String planDate = simpleDateFormat.format(margoBuildPlanVO.getPlanStartDate());
			args[0] = margoBuildPlanVO.getProjectId();
			args[1] = planDate;

			logger.info(Thread.currentThread().getName()+"**************"+planDate);
			List<MargoBuildPlanVO> lstMargoBuildPlanVO=null;
			try {
				lstMargoBuildPlanVO = JdbcTemplate.query(
						"SELECT * FROM sw_build_plan where sw_build_plan.project_id=? and DATE_FORMAT(sw_build_plan.plan_start_date, '%Y-%m-%d')=? ",
						args, rowMapper);	
			} catch (DataAccessException e) {
				logger.info(e.getMessage());
			}
			try {
				//延迟  演示效果
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if(lstMargoBuildPlanVO !=null && lstMargoBuildPlanVO.size()==0) {
				Object[] insertArgs = new Object[3];
				insertArgs[0] = margoBuildPlanVO.getBuildPlanId();
				insertArgs[1] = margoBuildPlanVO.getProjectId();
				insertArgs[2] = margoBuildPlanVO.getPlanStartDate();
				logger.info(insertArgs.toString());
				return JdbcTemplate.update(
						"insert into sw_build_plan(build_plan_id,project_id,plan_start_date) values(?,?,?)",
						insertArgs);	
			}

			if(lstMargoBuildPlanVO.size()>0) {
				return 2;
			}
			return 0;
		} finally {
			margoZookeeperLock.unlock(ownedArea);
		}

	}
	
	/**
	 * 	不上锁新增
	 * @param margoBuildPlanVO
	 * @return
	 */
	public int insertBuildPlan(MargoBuildPlanVO margoBuildPlanVO) {
		RowMapper<MargoBuildPlanVO> rowMapper = new BeanPropertyRowMapper<MargoBuildPlanVO>(MargoBuildPlanVO.class);
		Object[] args = new Object[2];

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String planDate = simpleDateFormat.format(margoBuildPlanVO.getPlanStartDate());
		args[0] = margoBuildPlanVO.getProjectId();
		args[1] = planDate;
		logger.info(Thread.currentThread().getName()+"**************"+planDate);
		List<MargoBuildPlanVO> lstMargoBuildPlanVO=null;
		try {
			lstMargoBuildPlanVO = JdbcTemplate.query(
					"SELECT * FROM sw_build_plan where sw_build_plan.project_id=? and DATE_FORMAT(sw_build_plan.plan_start_date, '%Y-%m-%d')=? ",
					args, rowMapper);	
		} catch (DataAccessException e) {
			logger.info("异常:"+e.getMessage());
		}
		try {
			//延迟  演示效果
			Thread.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		if(lstMargoBuildPlanVO !=null && lstMargoBuildPlanVO.size()==0) {
			Object[] insertArgs = new Object[3];
			insertArgs[0] = margoBuildPlanVO.getBuildPlanId();
			insertArgs[1] = margoBuildPlanVO.getProjectId();
			insertArgs[2] = margoBuildPlanVO.getPlanStartDate();
			logger.info("正常插入:"+insertArgs.toString());
			return JdbcTemplate.update(
					"insert into sw_build_plan(build_plan_id,project_id,plan_start_date) values(?,?,?)",
					insertArgs);	
		}
		if(lstMargoBuildPlanVO.size()>0) {
			return 2;
		}
		return 0; 
	}
}
